package com.exampletest.controller;

import com.exampletest.model.Enterprise;
import com.exampletest.model.User;
import com.exampletest.service.EnterpriseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/enterprise")
public class EnterpriseController {
    @Autowired
    private EnterpriseService enterpriseService;

    @GetMapping("/login")
    public String loginPage() {
        return "enterprise/login";
    }

    @PostMapping("/login")
    public String login(@RequestParam String account, @RequestParam String password, Model model, HttpSession session) {
        Enterprise enterprise = enterpriseService.login(account, password);
        if (enterprise != null) {
            session.setAttribute("enterprise", enterprise);
            return "redirect:/enterprise/dashboard";
        } else {
            model.addAttribute("error", "账号或密码错误");
            return "enterprise/login";
        }
    }

    @GetMapping("/register")
    public String registerPage(Model model) {
        List<Enterprise> enterprises = enterpriseService.getAllEnterprises();
        System.out.println("获取到的企业数量: " + enterprises.size()); // 调试日志
        model.addAttribute("enterprises", enterprises);
        return "enterprise/register";
    }

    @PostMapping("/register")
    public String register(Enterprise enterprise, Model model) {
        boolean success = enterpriseService.register(enterprise);
        if (success) {
            return "redirect:/enterprise/login";
        } else {
            model.addAttribute("error", "账号已存在");
            return "enterprise/register";
        }
    }

    @GetMapping("/dashboard")
    public String dashboard(HttpSession session, Model model) {
        if (session.getAttribute("enterprise") == null) {
            return "redirect:/enterprise/login";
        }
        return "enterprise/dashboard";
    }

    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/enterprise/login";
    }

    // 企业账号管理
    @GetMapping("/list")
    public String listEnterprises(@RequestParam(required = false) String keyword, Model model) {
        List<Enterprise> enterprises;
        if (keyword != null && !keyword.trim().isEmpty()) {
            enterprises = enterpriseService.searchEnterprises(keyword);
        } else {
            enterprises = enterpriseService.getAllEnterprises();
        }
        model.addAttribute("enterprises", enterprises);
        model.addAttribute("keyword", keyword);
        return "enterprise/list";
    }

    @GetMapping("/edit/{id}")
    public String editEnterprise(@PathVariable Integer id, Model model) {
        Enterprise enterprise = enterpriseService.getEnterpriseById(id);
        model.addAttribute("enterprise", enterprise);
        return "enterprise/edit";
    }

    @PostMapping("/update")
    public String updateEnterprise(Enterprise enterprise) {
        enterpriseService.updateEnterprise(enterprise);
        return "redirect:/enterprise/list";
    }

    @GetMapping("/delete/{id}")
    public String deleteEnterprise(@PathVariable Integer id) {
        enterpriseService.deleteEnterprise(id);
        return "redirect:/enterprise/list";
    }

//    @GetMapping("/all")
//    public List<Enterprise> getAllEnterprises() {
//        return enterpriseService.getAllEnterprises();
//    }

    @GetMapping("/status/{id}/{status}")
    public String toggleStatus(@PathVariable Integer id, @PathVariable Integer status) {
        enterpriseService.toggleStatus(id, status);
        return "redirect:/enterprise/list";
    }

    @GetMapping("/profile")
    public String profilePage(HttpSession session, Model model) {
        // 获取当前用户绑定的企业ID
        User user = (User) session.getAttribute("user");
        if (user == null) {
            return "redirect:/user/login";
        }

        // 获取企业信息
        Enterprise enterprise = enterpriseService.getEnterpriseById(user.getEnterpriseId());
        if (enterprise == null) {
            return "redirect:/index";
        }

        model.addAttribute("enterprise", enterprise);
        return "enterprise/profile";
    }

    @PostMapping("/update-profile")
    public String updateEnterpriseProfile(@ModelAttribute Enterprise enterprise, Model model) {
        try {
            enterpriseService.updateEnterprise(enterprise);
            model.addAttribute("success", "企业资料更新成功");
        } catch (Exception e) {
            model.addAttribute("error", "更新企业资料时出错: " + e.getMessage());
        }
        return "enterprise/profile";
    }

    @PostMapping("/verify")
    @ResponseBody
    public Map<String, Object> verifyEnterprise(@RequestBody Map<String, String> credentials, HttpSession session) {
        String account = credentials.get("account");
        String password = credentials.get("password");

        Enterprise enterprise = enterpriseService.login(account, password);
        Map<String, Object> response = new HashMap<>();

        if (enterprise != null) {
            // 验证成功，将企业信息存入session
            session.setAttribute("verifiedEnterprise", enterprise);
            response.put("success", true);
        } else {
            response.put("success", false);
            response.put("message", "企业账号或密码错误");
        }

        return response;
    }
}